import { defineStore } from 'pinia';
import { getCurrentUserUnreadCount } from '@/api/im'
import useUserStore  from './user';
const userStore = useUserStore();
export interface Im {
	timer: any;
    total: any;
} 
const useImStore = defineStore('Im', {
	state: () => ({
		total: 0,
        timer:null
	}),
	actions: {
		async setImInterval(){
            this.colseInterval();
            await getCurrentUserUnreadCount({
                userId:userStore.userId
            }).then((res)=>{
                if(res.code == 200){
                    this.total = res.data
                }
            })
            this.timer = setInterval(async ()=>{
                await getCurrentUserUnreadCount({
                    userId:userStore.userId
                }).then((res)=>{
                    if(res.code == 200){
                        this.total = res.data
                    }
                })
            },10000) as unknown as null;
        },
        colseInterval(){
            if(this.timer != null ){
                clearInterval(this.timer);
            }
        }
	}
});
export default useImStore;
